package com.zhiyou.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.entity.*;
import com.zhiyou.service.RecordService;
import com.zhiyou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/recordController")
public class RecordController {
    @Autowired
    RecordService recordService;
   /* @RequestMapping(value = "/getAll")
    public void getAll(){
        System.out.println("访问到......");
        List<Record> all = recordService.getAllRecord();
        System.out.println("all = " + all);
    }*/
    //显示公寓修缮信息  显示出来

    @RequestMapping("/getAll")
    @ResponseBody
    public MsgRecord getEmpsWithJson(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        PageHelper.startPage(pn,5);
        List<Record> records = recordService.getAllRecord();
        System.out.println(records.get(0).toString());
        PageInfo<Record> page = new PageInfo(records,5);
        //model.addAttribute("pageInfo",page);
        return MsgRecord.success().add("pageInfo",page);

    }

    //根据房屋名字查询房屋的修缮记录
    @RequestMapping("/getRecordByHouseRoom")
    public String getRecordByHouseName(String houseName, HttpSession session){
        //此方法返回一条 记录
        House house = new House();
        house.sethRoomno("111");
        List<Record> record = recordService.selectRecordByHouseRoom(house);
        return "red";
    }

    @RequestMapping("/getAllLogistics")
    @ResponseBody
    public MsgRecord getAllLogistics(){
        System.out.println("查询所有后勤人员");
        List<Logistics> logistics = recordService.getAllLogistics();
        System.out.println("logistics" + logistics);
        System.out.println("查询人员完毕");
        return MsgRecord.success().add("logistics",logistics);

    }

    @RequestMapping(value = "/getRecord/{id}",method = RequestMethod.GET)
    @ResponseBody
    public MsgRecord getRecord(@PathVariable("id") Integer id){
        Record record = recordService.getRecordById(id);
        return MsgRecord.success().add("record",record);

    }

    /*
    * 前端传回什么数据
    * houseroomno loid recordremark  reTime
    *
    * */

    @RequestMapping(value = "/addRecord",method = RequestMethod.POST)
    @ResponseBody
    public MsgRecord addRecord(Record record,String houseRoomno,String logisticsName){
        System.out.println("record的retime"+record.getReTime());
        System.out.println("=================");
        System.out.println("huseroomno"+houseRoomno + "logistics" + logisticsName);
        //根据房屋roomno查询houseid  根据修缮人员名字查询loid
        Logistics logistics = recordService.getLogisticsByName(logisticsName);
        //House house = recordService.getHouseByHouseRoomno(houseRoomno);
        House house =recordService.getHouseByHouseRoomno(houseRoomno);
        System.out.println("houseId" + house.gethId());
        //前端传来什么数据 (roomno,lonam,remark,retime)
        Record record1 = new Record();
        record1.sethId(house.gethId());
        record1.setLoId(logistics.getLoId());
        record1.setReRemark(record.getReRemark());
        record1.setReTime(record.getReTime());
        recordService.addRecord(record1);
        return MsgRecord.success();
    }
    //编辑
    @RequestMapping(value = "/updateRecord",method = RequestMethod.POST)
    @ResponseBody
    public MsgRecord updateRecord(Record record,String houseRoomno,String logisticsName){
        //record表修改 reReamrk reTime  houseroome不能修改 后勤人员也不能修改
        //根据id进行修改
        recordService.updateRecordById(record);
        return MsgRecord.success();
    }

    //模糊查询
    //后台要传来什么数据string  //
    @RequestMapping(value = "/selectRecords",method = RequestMethod.POST)
    @ResponseBody
    public MsgRecord selectRecords(String select_input,@RequestParam(value = "pn",defaultValue = "1")Integer pn){
        System.out.println("查询数据");
        System.out.println("select_input" + select_input);
        //record表修改 reReamrk reTime  houseroome不能修改 后勤人员也不能修改
        //根据id进行修改
        PageHelper.startPage(pn,5);
        List<Record> records = recordService.getRecordsRetimeOrHouseAreaOrLoName(select_input);
        System.out.println(records.get(0).toString());
        PageInfo<Record> page = new PageInfo(records,5);
        return MsgRecord.success().add("pageInfo",page);
    }

    //删除
    @RequestMapping(value = "/deleteReord",method = RequestMethod.POST)
    @ResponseBody
    public MsgRecord deleteReord(Record record){
        System.out.println("record"+record);
        System.out.println("执行删除");
        //根据id进行删除
        recordService.deleteRecordById(record);
        return MsgRecord.success();
    }

}
